//  File:         Event.cpp
//  Author:       Scott Nordmann & Lauren Wallace
//  Description:  Non-inline member function definitions.
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "Event.h"

using namespace std;

Event::Event(const string& crtr) : Entry(crtr)
{
	hour = -1;
	minute = -1;
}

void Event::populate()
{
	Entry::populate();
	
	char schedule;
	string time;

	cout << "Schedule this event for a certain time (y/n)? ";
	cin >> schedule;
	cin.ignore();

	if (toupper(schedule) == 'Y')
	{
		string timeString;

		cout << "Enter time (HH:MM AM): ";
		getline(cin, timeString);

		setTime(timeString);
	}
}

string Event::getTimeString() const
{
	string time = "";

	if (getHour() != -1)
	{
		ostringstream oss;

		oss << getHour() << ":" << getMinute() << " " << (isAM ? "AM" : "PM");

		time = oss.str();
	}

	return time;
}

void Event::edit()
{
	char command = ' ';
	string input;
	string timeString;

	system("cls");

	while (toupper(command) != 'N')
	{
		cout << "Editing a Note" << endl << endl;

		display();

		cout << endl;
		
		cout << "T: Edit Title" << endl;
		cout << "C: Edit Content" << endl;
		cout << "M: Edit Time" << endl;
		cout << "N: No More Changes" << endl << endl;
		
		cout << "Please enter your selection: ";
		cin >> command;
		cin.ignore();

		cout << endl;

		switch (toupper(command))
		{
		case 'T':
			cout << "Old Title: " << getTitle() << endl;
			cout << "New Title: ";
			getline(cin, input);
			setTitle(input);
			break;
		
		case 'C':
			cout << "Old Content: " << getContent() << endl;
			cout << "New Content: ";
			getline(cin, input);
			setContent(input);
			break;

		case 'M':
			if(getHour() > -1)
			{
				cout << "Old Time: ";
				displayTime();
				cout << endl;
			}
			else
			{
				cout << "Old Time: No Time Scheduled" << endl;
			}

			cout << "New Time (HH:MM AM): ";
			getline(cin, timeString);

			setTime(timeString);
			break;
		
		case 'N':
			break;

		default:
			cout << "Invalid selection." << endl << endl;
			break;
		}

		system("cls");
	}

}

void Event::displayTime() const
{
	if (getHour() > -1)
	{
		cout.fill('0');
		cout << right << setw(2) << getHour() << ":" << setw(2) << getMinute() << " ";

		if (isAM)
		{
			cout << "AM";
		}
		else
		{
			cout << "PM";
		}
	}
	else
	{
		cout << "     ";
	}
}

void Event::displaySummary()
{
	displayTime();
	cout << " ";
	Entry::displaySummary();
}

void Event::display() const
{
	Entry::display();

	cout << "Scheduled at: ";
	displayTime();
	cout << endl;
}

void Event::setTime(const string& timeString)
{
	istringstream iss;

	string hstring;
	string mstring;
	string astring;

	iss.str(timeString);

	getline(iss, hstring, ':');
	getline(iss, mstring, ' ');
	getline(iss, astring);

	int hour = 0;
	int minute = 0;
	bool am = false;

	iss.clear();

	iss.str(hstring);
	iss >> hour;

	iss.clear();

	iss.str(mstring);
	iss >> minute;

	iss.clear();

	if (toupper(astring[0]) == 'A' && toupper(astring[1]) == 'M')
	{
		am = true;
	}

	setTime(hour, minute, am);
}